package cn.tj.wenda.cloud.other.rest.impl;

import cn.tj.wenda.cloud.bas.rest.BasTenantRest;
import cn.tj.wenda.cloud.entity.LoginUserInfo;
import cn.tj.wenda.cloud.entity.ResponseBean;
import cn.tj.wenda.cloud.exception.BusinessException;
import cn.tj.wenda.cloud.other.entity.Books;
import cn.tj.wenda.cloud.other.rest.BookRest;
import cn.tj.wenda.cloud.other.service.BookService;
import cn.tj.wenda.cloud.util.EntityUtil;
import cn.tj.wenda.cloud.util.LoginUser;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author Wenda
 */
@RestController
@Slf4j
public class BookRestImpl implements BookRest {

    @Autowired
    BookService bookService;

    @Autowired
    EntityUtil entityUtil;

    @Autowired
    BasTenantRest basTenantRest;


    @SuppressWarnings("unused")
	@Override
    public ResponseBean getBookList(int page, int limit, String params) {
        ResponseBean res = new ResponseBean();

        try {
            LoginUserInfo loginUserInfo = LoginUser.getLoginUserInfo();

            PageInfo<Books> pageInfo = bookService.getBookList(page,limit,params);
            entityUtil.addPageInfo(res, pageInfo);

            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            HttpServletRequest request = servletRequestAttributes.getRequest();
            Enumeration<String> headers = request.getHeaderNames();

            //int i=1;
            //调候Bas传递Token测试
            res = basTenantRest.getBasTenantList(page,limit,"{}");
            res.setErr("555555555555555555");
        }
        catch(BusinessException e) {
            res.setErr("1");
            res.setErr(e.getErrMsg());
        }
        catch(Exception ex) {
            res.setErr("1");
            res.setErr(ex.getLocalizedMessage());
            log.error("获取图书信息时出错",ex);
        }

        return res;
    }

    @Override
    public ResponseBean updateBook(@PathVariable(name="bookid",required = true) String bookId, @RequestBody Books book) {
        ResponseBean res = new ResponseBean();
        try {
            int ret = bookService.updateBook(bookId, book);
            res.setData(ret);
        }
        catch(BusinessException e) {
            res.setErr("1");
            res.setErr(e.getErrMsg());
        }
        catch(Exception ex) {
            res.setErr("1");
            res.setErr(ex.getLocalizedMessage());
            log.error("更新图书信息时出错",ex);
        }
        return res;
    }

    @Override
    public ResponseBean insertBook(@RequestBody Books book) {
        ResponseBean res = new ResponseBean();
        try {
            int ret = bookService.insertBook(book);
            res.setData(ret);
        }
        catch(BusinessException e) {
            res.setErr("1");
            res.setErr(e.getErrMsg());
        }
        catch(Exception ex) {
            res.setErr("1");
            res.setErr(ex.getLocalizedMessage());
            log.error("新增图书信息时出错",ex);
        }
        return res;
    }

    @Override
    public ResponseBean deleteBook(String bookId) {
        ResponseBean res = new ResponseBean();
        try {
            int ret = bookService.deleteBook(bookId);
            res.setData(ret);
        }
        catch(BusinessException e) {
            res.setErr("1");
            res.setErr(e.getErrMsg());
        }
        catch(Exception ex) {
            res.setErr("1");
            res.setErr(ex.getLocalizedMessage());
            log.error("删除图书信息时出错",ex);
        }
        return res;
    }
}
